package edu.gatech.oad.yuno;
import android.app.Activity;

import android.content.SharedPreferences;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
/**
 * 
 * @author Krista
 *
 */

public class AddLocationActivity extends Activity implements View.OnClickListener{
	private Button addLocation;
	private Button cancelLocation;
	private EditText editLocationName;
	private EditText editStAddress;
	private EditText editCity;
	private EditText editState;
	private EditText editZipcode;
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_location);
		editLocationName = (EditText) findViewById(R.id.editLocationName);
		editStAddress = (EditText) findViewById(R.id.editLocationAddress);
		editCity = (EditText) findViewById(R.id.editLocationCity);
		editState = (EditText) findViewById(R.id.editLocationState);
		editZipcode = (EditText) findViewById(R.id.editLocationZipcode);
		addLocation = (Button) findViewById(R.id.addLocationButton);
		cancelLocation = (Button) findViewById(R.id.locationCancelButton);
		addLocation.setOnClickListener(this);
		cancelLocation.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		if (v == addLocation){
				Location location = new Location();
				SharedPreferences prefs = getSharedPreferences("YUNoDos", MODE_PRIVATE);
				location.setUser(prefs.getLong("logged_in_user_id", 0));
				String locationName = editLocationName.getText().toString();
				location.setName(locationName);
				String stAddress = editStAddress.getText().toString() + "\n";
				String city = editCity.getText().toString() + "\n";
				String state = editState.getText().toString() + "\n";
				String zipcode = editZipcode.getText().toString();
				String address = stAddress + city + state + zipcode;
				location.setAddress(address);
				try{
					location.validate();
					location.save();
					finish();
					Toast.makeText(getApplicationContext(), editLocationName.getText().toString() +" saved" ,
						Toast.LENGTH_SHORT).show();
				}
				catch (ValidationException e){
					Toast.makeText(this, e.getMessage(),
		    				Toast.LENGTH_SHORT).show();					
				}
			}			
		else if (v == cancelLocation){
			finish();
		}
		
	}
	
	private boolean requiredFieldsFilled(){
		if (editLocationName.getText().toString().trim().length() == 0){
			return false;
		}
		else if (editStAddress.getText().toString().trim().length() == 0){
			return false;
		}
		else if (editCity.getText().toString().trim().length() == 0){
			return false;
		}
		else if (editState.getText().toString().trim().length() == 0){
			return false;
		}
		else if (editZipcode.getText().toString().trim().length() == 0){
			return false;
		}
		else {
			return true;
		}
	}

}
